<?php
/**
 * 景点
 */
namespace App\Http\Controllers\Admin\Member;

use App\Http\Controllers\Admin\Member\Service\Member;
use App\Http\Controllers\Admin\Member\Repository\Member as MemberRepository;
use App\Http\Controllers\Admin\Scene\Service\SceneBuy;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class MemberController extends Controller
{
    private $manager;
    private $repository;
    private $request;
    private $viewPrefix = 'admin.member.member.';
    private $routePrefix = 'admin.member.member';
    public function __construct (Request $request)
    {
        $this->manager = new Member();
        $this->repository = new MemberRepository();
        $this->request = $request;
    }
    public function jingdian(Request $request){

        $member=\App\Member::find($request->user_id);

        return view('admin.member.member.jingdian',compact('member'));
    }
    public function jingdianUpdate(Request $request){
        $member=\App\Member::find($request->user_id);
        $member->wallet_spots=$request->wallet_spots;
        $member->save();
        return redirect('/admin/member/member')->with(
            $this->success()
        );

    }
    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{

            return view($this->viewPrefix.'index',[
                'status' =>    $this->manager->getStatusList(),
                'sex' =>    $this->manager->getSexList(),
                'role' =>    $this->manager->getRoleList(),
                'subscribe' =>  $this->manager->getSubscribeList(),
                'manager'   =>  $this->manager,
                'routePrefix'   =>  $this->routePrefix,

            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){
        return $this->page_json_render($this->manager->getPageList($this->request));
    }
    public function distribute($id){
        $model = $this->manager->getMember($id);
        $tree = $this->manager->getMemberPath($id);
        if($tree){
            $tree[] = $model->toArray();
        }else{
            $tree[] = $model;
        }
        return view($this->viewPrefix.'distribute',[
            'status' =>    $this->manager->getStatusList(),
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix,
            'model' =>  $model,
            'tree'  =>  $tree,
            'teamPushMoney' =>  $this->manager->getSumPath($id)
        ]);
    }
    public function setWorker(Request $request){
        $id=$request->id;
        $member=\App\Http\Controllers\Admin\Member\Repository\Member::find($id);
        $member->is_worker=1;
        $member->save();
        return ['info'  =>  'success'];
    }
    public function noWorker(Request $request){
        $id=$request->id;
        $member=\App\Http\Controllers\Admin\Member\Repository\Member::find($id);
        $member->is_worker=0;
        $member->save();
        return ['info'  =>  'success'];
    }
    public function setDisplay(Request $request){
        $id=$request->id;
        $member=\App\Http\Controllers\Admin\Member\Repository\Member::find($id);
        $member->is_display=1;
        $member->save();
        return ['info'  =>  'success'];
    }
    public function noDisplay(Request $request){
        $id=$request->id;
        $member=\App\Http\Controllers\Admin\Member\Repository\Member::find($id);
        $member->is_display=0;
        $member->save();
        return ['info'  =>  'success'];
    }
    /**
     * 已购买景点
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function scene($id){
        if(!feature_set('Scene')){
            throw new \Exception('缺少依赖模块:Scene');
        }
        $manager = new SceneBuy();
        $model = $this->manager->getMember($id);
        $list = $manager->getSceneBuyByMember($id);
        return view($this->viewPrefix.'scene',[
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix,
            'model' =>  $model,
            'list'  =>  $list,
        ]);
    }
    /**
     * @return array
     */
    private function draft(){
        $this->manager->draft($this->request->id);
        return  ['info'  =>  'success','msg'    =>  $this->request->id];
    }

    /**
     * @return array
     */
    private function published(){
        $this->manager->published($this->request->id);
        return  ['info'  =>  'success'];
    }

    private function travel(){
        $member = $this->manager->getMember($this->request->id);
              if($member->pid){
            return ['info'=>'error','msg'=>'有推广人的游客不能成为旅行社'];
        }
        $this->manager->travel($this->request->id);
        return  ['info'  =>  'success'];
    }
    private function visitor(){
        $this->manager->visitor($this->request->id);
        return  ['info'  =>  'success'];
    }
    /**
     * @return array
     */
    private function delete(){
        $this->manager->del($this->request->id);
        return  ['info'  =>  'success'];
    }

    private function sortCol(){
        $this->manager->sortCol($this->request->id,$this->request->sort);
        return  ['info'  =>  'success'];
    }
    public function exportList(){

        $cellData = $this->manager->exportList($this->request);
        Excel::create('用户列表',function($excel) use ($cellData){
            $excel->sheet('orders', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
    }


}
